package model.forview;

import java.util.List;

import model.entityes.exception.ConstantValueException;
import models.ConstantName;
import models.ConstantValue;

public class IndexModelForView extends ContentForMainTemplate
{
    public String description;
    public String loginTitle;
    public String registrationTitle;
    public String recoveryPassword;
    public String loginWellHeader;
    //public MainModelForView mainModelForView;

    public IndexModelForView()
    {
	String title = getTitleFromDB();
	String userName = "";
	String header = getHeaderFromDB();
	String footer = getFooterFromDB();
	this.mainModelForView = new MainModelForView(title, userName, header,
		footer);
	setDefaultValue();
    }

    public void setDefaultValue()
    {
	this.description = getDescriptionFromDB();
	this.loginTitle = getLoginTitleFromDB();
	this.registrationTitle = getRegistrationTitleFromDB();
	this.recoveryPassword = getRecoveryPasswordFromDB();
	this.loginWellHeader = getLoginWellHeaderFromDB();
    }

    public String getDescriptionFromDB()
    {
	String standartValue = "описание";
	String constName = ConstantName.INDEX_DESCRIPTION;

	try
	{
	    List<ConstantValue> descConst = ConstantValue
		    .searchByKey(constName);
	    if (descConst.size() == 1) standartValue = descConst.get(0)
		    .getValue();
	}
	catch (ConstantValueException e)
	{
	    e.printStackTrace();
	}

	return standartValue;
    }

    public String getLoginTitleFromDB()
    {
	String standartValue = "Войти";
	String constName = ConstantName.LOGIN_TITLE;

	try
	{
	    List<ConstantValue> descConst = ConstantValue
		    .searchByKey(constName);
	    if (descConst.size() == 1) standartValue = descConst.get(0)
		    .getValue();
	}
	catch (ConstantValueException e)
	{
	    e.printStackTrace();
	}

	return standartValue;

    }

    public String getRegistrationTitleFromDB()
    {
	String standartValue = "Регистрация";
	String constName = ConstantName.REGISTRATION_TITLE;

	try
	{
	    List<ConstantValue> descConst = ConstantValue
		    .searchByKey(constName);
	    if (descConst.size() == 1) standartValue = descConst.get(0)
		    .getValue();
	}
	catch (ConstantValueException e)
	{
	    e.printStackTrace();
	}

	return standartValue;
    }

    public String getRecoveryPasswordFromDB()
    {
	String standartValue = "Восстановить пароль";
	String constName = ConstantName.RECOVERY_PASSWORD;

	try
	{
	    List<ConstantValue> descConst = ConstantValue
		    .searchByKey(constName);
	    if (descConst.size() == 1) standartValue = descConst.get(0)
		    .getValue();
	}
	catch (ConstantValueException e)
	{
	    e.printStackTrace();
	}

	return standartValue;
    }

    public String getLoginWellHeaderFromDB()
    {
	String standartValue = "Пользователи";
	String constName = ConstantName.LOGIN_WELL_HEADER;

	try
	{
	    List<ConstantValue> descConst = ConstantValue
		    .searchByKey(constName);
	    if (descConst.size() == 1) standartValue = descConst.get(0)
		    .getValue();
	}
	catch (ConstantValueException e)
	{
	    e.printStackTrace();
	}

	return standartValue;
    }

    public String getTitleFromDB()
    {
	String standartValue = "Index";
	String constName = ConstantName.INDEX_TITLE;

	try
	{
	    List<ConstantValue> descConst = ConstantValue
		    .searchByKey(constName);
	    if (descConst.size() == 1) standartValue = descConst.get(0)
		    .getValue();
	}
	catch (ConstantValueException e)
	{
	    e.printStackTrace();
	}

	return standartValue;
    }

    public String getHeaderFromDB()
    {
	String standartValue = "Header";
	String constName = ConstantName.INDEX_HEADER;
	try
	{
	    List<ConstantValue> descConst = ConstantValue
		    .searchByKey(constName);
	    if (descConst.size() == 1) standartValue = descConst.get(0)
		    .getValue();
	}
	catch (ConstantValueException e)
	{
	    e.printStackTrace();
	}

	return standartValue;
    }

    public String getFooterFromDB()
    {
	String standartValue = "Footer";
	String constName = ConstantName.INDEX_FOOTER;

	try
	{
	    List<ConstantValue> descConst = ConstantValue
		    .searchByKey(constName);
	    if (descConst.size() == 1) standartValue = descConst.get(0)
		    .getValue();
	}
	catch (ConstantValueException e)
	{
	    e.printStackTrace();
	}

	return standartValue;
    }
}
